﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business.IManager;
using Business.Manager;
using System.Data;
using Utility;
using Entity;
using System.Data.SqlClient;

namespace OnlinePayment.Home
{
    public partial class GuestbookDetail : System.Web.UI.Page
    {
        IGuestbookManager guestbookManager = new GuestbookManager();
        public GuestBook gb;
        public DataTable dt;
        public string entityid;
        public int dtNum = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (object.Equals(Request["EntityId"], null))
            {
                // Response.Write(UtilityClass.MessageBox("错误"));
            }
            else
            {
                entityid = Request["EntityId"].ToString();
                ViewState["EntityId"] = entityid;
                gb = guestbookManager.getDetail(entityid);
                dataBind();
                   
            }

            if (!IsPostBack)
            {
                refleshViewdNum(entityid);
            }
               
        }

        private void refleshViewdNum(string entityid)
        {
            string commandText = "update GuestBook set TotalViews = TotalViews + 1 where EntityId = @entityid";
            SqlParameter[] paras = new SqlParameter[]
            {
               new SqlParameter("@entityid", entityid)
            };
            SQLHelper.ExecuteNonQuery(commandText, paras);
        }

        private void dataBind()
        {

            dt = guestbookManager.getReplys(entityid);
            
            gbdl.DataSource = dt;
            gbdl.DataBind();
            /*paging
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = dt.DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 3;

            int currentPage = Convert.ToInt32(Request["page"]);

            pds.CurrentPageIndex = currentPage;

            pds.CurrentPageIndex = currentPage;

            if (!pds.IsFirstPage)
            {
                //lnkUp.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1);
            }
            if (!pds.IsLastPage)
            {
                //lnkDown.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1);
            }

            //lbl_info.Text = "第" + (currentPage + 1) + "页，共" + pds.PageCount + "页";
            gbrpt.DataSource = pds;
            gbrpt.DataBind();*/

        }


      
        /*回复*/
        protected void lnkPublish_Click(object sender, EventArgs e)
        {
            GuestBookReply gbr = new GuestBookReply();
            gbr.EntityId = Convert.ToInt32(ViewState["EntityId"].ToString());
            gbr.ReplierName = txtUserName.Text.ToString();
            gbr.Body = txtContent.Text.ToString();
            guestbookManager.reply(gbr);
            dataBind();
            refleshPosts(gbr.EntityId);
        }

        private void refleshPosts(int entityId){
            SqlCommand command = SQLHelper.GetCommandProc("sp_update_posts");
            command.Parameters.Add("@entityId", SqlDbType.Int);
            command.Parameters["@entityId"].Value = entityId;
            try
            {
                command.Connection.Open();
            }
            catch
            {
                command.Connection.Close();
            }
            int rowsAffected = command.ExecuteNonQuery();
        }

        protected void btn_AckPay_Click(object sender, EventArgs e)
        {
            Response.Redirect("GuestbookList.aspx");
        }

        
    }
}